import { action, observable, computed } from 'mobx';
import { message } from 'antd';
import * as API from "../../apis/performance/programListApi";
import { accessAuditProgramNumApi } from '../../apis/performance/accessAuditListApi'
import { accessAssessmentCycleApi } from "../../apis/performance/accessResultListApi";
import { WeaTableNew } from "comsMobx";
import { idsAndNamesToArr } from '../../util/Util'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-方案设置查询
 * jiansong 2019/6/11
 */
const { TableStore } = WeaTableNew;
export default class ProgramListStore {

    @observable loading = true; //加载中
    @observable tableStore = new TableStore(); // new table
    @observable showSearchAd = false; // 高级搜索面板显示
    @observable stabList = []; //是否启用考核周期列表

    //考核方案未设置提醒数量
    @observable amount1 = 0; //年度
    @observable amount2 = 0; //半年
    @observable amount3 = 0; //季度
    @observable amount4 = 0; //月度

    @computed get hrmData() { //人力资源回显数据
        return idsAndNamesToArr(this.queryParams.hrmids, this.queryParams.hrmnames);
    };
    @computed get subcompanyData() { //分部回显数据
        return idsAndNamesToArr(this.queryParams.subcompanyids, this.queryParams.subcompanynames);
    };
    @computed get departmentData() { //部门回显数据
        return idsAndNamesToArr(this.queryParams.departmentids, this.queryParams.departmentnames);
    };
    @computed get queryType() { //类型
        let type = [];
        this.stabList.forEach(item => {
            if (item.key == 1) {
                type.push({ key: "nomonth", showname: getLabel('509524', '无月度') })
            } else if (item.key == 2) {
                type.push({ key: "noquarter", showname: getLabel('508861', '无季度') })
            } else if (item.key == 3) {
                type.push({ key: "nohyear", showname: getLabel('508862', '无半年') })
            } else if (item.key == 4) {
                type.push({ key: "nofyear", showname: getLabel('508865', '无年度') })
            }
        });
        return type;
    };

    @observable queryParams = {  //查询条件
        hrmids: null, //人员
        hrmnames: null, //人员名称
        subcompanyids: null, //分部
        subcompanynames: null, //分部名称
        departmentids: null, //部门
        departmentnames: null, //部门名称
        type: null, //类型
    };

    @action("重置查询条件")
    resetSearchAd = () => {
        this.queryParams.hrmids = null; //人员
        this.queryParams.hrmnames = null; //人员名称
        this.queryParams.subcompanyids = null; //分部
        this.queryParams.subcompanynames = null; //分部名称
        this.queryParams.departmentids = null; //部门
        this.queryParams.departmentnames = null; //部门名称
        this.queryParams.type = null; //类型
    };

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("初始化操作")
    doInit = () => {
        this.queryParams.hrmids = null;
        this.queryParams.hrmnames = null;
        this.queryParams.subcompanyids = null;
        this.queryParams.subcompanynames = null;
        this.queryParams.departmentids = null;
        this.queryParams.departmentnames = null;
        this.getaccessAssessmentCycleData();
        this.getAccessAuditProgramNumData();
    }

    @action("考核方案未设置数量")
    getAccessAuditProgramNumData = (params) => {
        this.loading = true;
        accessAuditProgramNumApi(params).then(action(res => {
            if (res.api_status) {
                this.amount1 = res.amount1;
                this.amount2 = res.amount2;
                this.amount3 = res.amount3;
                this.amount4 = res.amount4;
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

    @action("是否启用考核周期")
    getaccessAssessmentCycleData = () => {
        this.loading = true;
        accessAssessmentCycleApi().then(action(res => {
            if (res.api_status) {
                this.stabList = res.stabList;
                this.getProgramList();
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }))
    };

    @action("渲染table数据")
    getProgramList = () => {
        this.loading = true;
        API.programListApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                this.tableStore.getDatas(res.datas, 1);
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    }

    @action
    setShowSearchAd = bool => this.showSearchAd = bool;

    @action("搜索")
    doSearch = () => {
        this.getProgramList();
        this.showSearchAd = false;
    }

}